package com.usabb.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.usabb.domain.CombosCategoryDO;
import com.usabb.pojo.vo.CombosAddVO;
import com.usabb.pojo.vo.CombosNewAddVO;
import com.usabb.service.CombosCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.usabb.domain.MaternityCombosDO;
import com.usabb.service.MaternityCombosService;
import com.ifast.common.utils.Result;

/**
 * <pre>
 * 月子中心 套餐表
 * </pre>
 * <small> 2019-08-19 18:03:27 | Aron</small>
 */
@Slf4j
@Controller
@RequestMapping("/usabb/maternityCombos")
public class MaternityCombosController extends BaseController {
    @Autowired
    private MaternityCombosService maternityCombosService;
    @Autowired
    private CombosCategoryService combosCategoryService;

    @GetMapping()
    @RequiresPermissions("usabb:maternityCombos:maternityCombos")
    String MaternityCombos() {
        return "usabb/maternityCombos/maternityCombos";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("usabb:maternityCombos:maternityCombos")
    public Result<Page<MaternityCombosDO>> list(MaternityCombosDO maternityCombosDTO) {
        Long baseId = maternityCombosDTO.getBaseId();
        if (Objects.isNull(baseId)) {
            log.error("input baseId is null. so return.");
        }

        Wrapper<MaternityCombosDO> wrapper = new EntityWrapper<>(maternityCombosDTO).orderBy("id", false);
        Page<MaternityCombosDO> page = maternityCombosService.selectPage(getPage(MaternityCombosDO.class), wrapper);

        //page.getRecords().stream().map(it-> )

        return Result.ok(page);
    }

    @GetMapping("/add/{baseId}")
    @RequiresPermissions("usabb:maternityCombos:add")
    String add(@PathVariable("baseId") Long baseId, Model model) {
        model.addAttribute("baseId", baseId);

        List<CombosCategoryDO> categoryDOList = combosCategoryService.getByBizIdWithItems(baseId);
        model.addAttribute("categoryDOList", categoryDOList);

        return "usabb/maternityCombos/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("usabb:maternityCombos:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        MaternityCombosDO maternityCombos = maternityCombosService.getByIdWith(id);
        model.addAttribute("maternityCombos", maternityCombos);
        return "usabb/maternityCombos/edit";
    }

    @Deprecated
    @Log("添加月子中心 套餐")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("usabb:maternityCombos:add")
    public Result<String> save(CombosAddVO combosAddVO) {
        log.info("Add input: {}", combosAddVO);
        maternityCombosService.addCombos(combosAddVO);
        return Result.ok();
    }

    @Log("添加月子中心 套餐新增")
    @ResponseBody
    @PostMapping("/save-value")
    @RequiresPermissions("usabb:maternityCombos:add")
    public Result<String> saveItemValue(CombosNewAddVO addVO) {
        log.info("Add input: {}", addVO);
        maternityCombosService.addCombos(addVO);
        return Result.ok();
    }

    @Deprecated
    @Log("修改月子中心 套餐表")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("usabb:maternityCombos:edit")
    public Result<String> update(MaternityCombosDO maternityCombos) {
        boolean update = maternityCombosService.updateById(maternityCombos);
        return update ? Result.ok() : Result.fail();
    }

    @Log("修改月子中心 套餐表")
    @ResponseBody
    @RequestMapping("/update-value")
    @RequiresPermissions("usabb:maternityCombos:edit")
    public Result<String> updateValue(CombosNewAddVO addVO) {
        boolean update = maternityCombosService.updateValue(addVO);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除月子中心 套餐表")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("usabb:maternityCombos:remove")
    public Result<String> remove(Long id) {
        maternityCombosService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除月子中心 套餐表")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("usabb:maternityCombos:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        maternityCombosService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
